package logic

import (
	"context"
	"fmt"
	"shop_srv/goods"
	"shop_srv/internal/svc"
	"shop_srv/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodsLogic {
	return &GetGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetGoodsLogic) GetGoods(in *goods.GetGoodsRequest) (*goods.GetGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{}
	list, err := g.GetList(int(in.Page), int(in.Size))
	if err != nil {
		fmt.Println("列表查询失败")
		return nil, err
	}
	var getGoods []*goods.GetGoods

	for _, v := range list {
		g2 := goods.GetGoods{
			GoodsTitle:  v.GoodsTitle,
			GoodsPrice:  v.GoodsPrice,
			GoodsStock:  int64(v.GoodsStock),
			GoodsCover:  v.GoodsCover,
			GoodsStatus: int64(v.GoodsStatus),
		}
		getGoods = append(getGoods, &g2)
	}
	return &goods.GetGoodsResponse{GetGoods: getGoods}, nil
}
